package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.R.layout;
import com.apiit.webserviceclient.R.menu;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;
import com.apiit.webserviceclient.utility.GlobalData;

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class StudentTimeTableTab extends Activity implements OnClickListener {

	Spinner batchSpinner;
	DatePicker startDate, endDate;
	String selectedBatch;
	Button go;
	int day, month, year;
	int endday, endmonth, endyear;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_student_time_table_tab);
		
		startDate = (DatePicker) findViewById(R.id.startdatePicker);
		endDate = (DatePicker) findViewById(R.id.enddatePicker);

		batchSpinner = (Spinner) findViewById(R.id.batchSpinner);
		batchSpinner.setOnItemSelectedListener(new StateOnItemSelectedListener());
		
		go = (Button) findViewById(R.id.timetableSearch);
		go.setOnClickListener(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.student_time_table_tab, menu);
		return true;
	}

	public class StateOnItemSelectedListener implements OnItemSelectedListener {
		public void onItemSelected(AdapterView<?> parent, View itemSelected,
				int pos, long id) {
			selectedBatch = batchSpinner.getItemAtPosition(pos).toString();
			Log.d("charini", selectedBatch);

		}

		public void onNothingSelected(AdapterView<?> parent) { // Do nothing
																// here
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	    day = startDate.getDayOfMonth();
	    month = startDate.getMonth() + 1;
	    year = startDate.getYear();
	    Log.d("DAY", "day" +day);
	    Log.d("month", "month" +month);
	    Log.d("year", "year" +year);
	    
	    endday = endDate.getDayOfMonth();
	    endmonth = endDate.getMonth() + 1;
	    endyear = endDate.getYear();
	    
	    String dateFormat = day +"%2F"+month+"%2F"+"13";
	    String endDateFormat = endday +"%2F"+endmonth+"%2F"+"13" ;
	    
		Toast.makeText(this, "Button is clicked" + selectedBatch + day + month+ year,
				Toast.LENGTH_LONG).show();

		BPEL_Process bpelProcess2 = new BPEL_Process();
		BPEL_Request request2 = new BPEL_Request();
		request2.setUrl("http://timetable.apiit.lk/TT-Version17.0/viewbybatch.xhtml");

		Map<String, String> xpathMap2 = new HashMap<String, String>();
		xpathMap2.put("text", "//redirect---url");
		request2.setXpathMap(xpathMap2);
		Map<String, String> parametersMap2 = new HashMap<String, String>();
		parametersMap2.put("javax.faces.partial.ajax", "true");
		parametersMap2.put("javax.faces.source", "j_idt16%3Aj_idt28");
		parametersMap2.put("javax.faces.partial.execute", "%40all");
		parametersMap2.put("j_idt16%3Aj_idt28", "j_idt16%3Aj_idt28");
		parametersMap2.put("j_idt16", "j_idt16");
		parametersMap2.put("j_idt16%3Anow_input", selectedBatch);
		parametersMap2.put("j_idt16%3Aj_idt24_input", endDateFormat);
		parametersMap2.put("j_idt16%3Aj_idt26_input", dateFormat);
		parametersMap2.put("javax.faces.ViewState","H4sIAAAAAAAAAFvzloG1oLiIQTArsSxRr7QkM0fPI7E4wzexgJX91sHDYgkXmRmY3Bi4cvITU9wSk0vyizwZOEsyilKLM%2FJzUioK7B0YQICnnANICgAxSwkDf1Z8ZkqJoZkVmDYyKS1iEI72AVuQk5iXrueflJWaXGI94VzEfIFizRwmBoaKApDOAiAoYeCG6c7LLy8tZKhjYEbIsYLkDOGiTHAWK0gezmOuK2LQBdlXoZeWmJxarJecn1uQn5eaV6IX6hmWmVoelJ9fohJQlF%2BQWlRS6Z1aWcwABUJAtxQx8CHc6ppXmossCXQEW05icYlnCjzQwOo880pS01OLhB4tWPK9sd2CiYHRk4G1LDGnNBVongBCnV9pblJqUduaqbLcUx50w7zOBvIcWrCZoXq%2BAgBmX6JnqgEAAA%3D%3D");
		request2.setParametersMap(parametersMap2);
		request2.setType("POST");
		BPEL_Response response2 = null;
		try {
			response2 = bpelProcess2.doRequest(request2);

		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			String testResult[] = response2.getResultsMap().get("text").split(";");
		
		BPEL_Process bpelProcess3 = new BPEL_Process();
		BPEL_Request request3 = new BPEL_Request();
		request3.setUrl("http://timetable.apiit.lk"+testResult[0]);

		Map<String, String> xpathMap3 = new HashMap<String, String>(); ///html[1]/body[1]/div[2]/div[1]/div[1]/table[1]/thead[1]/tr[2]/th[5]/div[1]/span[1]
		xpathMap3.put("text", "/html[1]/body[1]/div[2]/div[1]/div[1]/table[1]/thead[1]/tr[1]/th[1]/table[1]---");
		request3.setXpathMap(xpathMap3);
		Map<String, String> parametersMap3 = new HashMap<String, String>();
		request3.setParametersMap(parametersMap3);
		request3.setType("GET");
		BPEL_Response response3 = null;
		try {
			response3 = bpelProcess3.doRequest(request3);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		if (response3.getStatus().equalsIgnoreCase(GlobalData.message_success)) {
			String testResult1 = response3.getResultsMap().get("text");

			Toast.makeText(getApplicationContext(), testResult1 + selectedBatch,
					Toast.LENGTH_LONG).show();
			Intent i = new Intent(this, DisplayTimetable.class);
			i.putExtra("text", testResult1);
			startActivity(i);

		}

	}

}
